package com.kun.service;

import com.kun.entity.Attachment;
import com.kun.mapper.AttachmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AttachmentServiceImpl implements AttachmentService {

    @Autowired
    private AttachmentMapper attachmentMapper;

    @Override
    public int addAttachment(Attachment attachment) {
        return attachmentMapper.addAttachment(attachment);
    }

    @Override
    public int deleteAttachment(int id) {
        return attachmentMapper.deleteAttachment(id);
    }

    @Override
    public int updateAttachment(Attachment attachment,Map map) {
        if (map.containsKey("changeDelete")){
            attachment.setEnable(null);
        }
        if (map.containsKey("changeEnable")){
            attachment.setIsDelete(null);
        }

        return attachmentMapper.updateAttachment(attachment);
    }

    @Override
    public List<Attachment> selectAllAttachments(Map map) {
        if (map.containsKey("pageIndex")&&map.containsKey("pageSize")){
            Integer pageIndex = (Integer) map.get("pageIndex");
            Integer pageSize= (Integer) map.get("pageSize");
            pageIndex=pageSize*(pageIndex-1);
            map.replace("pageIndex",pageIndex);
        }
        return attachmentMapper.selectAllAttachments(map);
    }
}
